let initdata = {
    arr:[],// 请求数据渲染得数组，首页里的数组
    shopArr:[] ,// 这个数组是放购物车数据的，
    checkall:false, // 购物车全选得变量
    orderlist:[], // 发货状态数据
    adrressarr:[{
        name:"zhangsan",
        phone:"1223456543",
        address:"北京市昌平区"
    }]
} 
let shopReducer = (state = initdata,{type,payload} )=>{
    let newArr = JSON.parse(JSON.stringify(state.arr)) // 做一个深拷贝，为了不影响原来的state.arr
    let newShoparr = JSON.parse(JSON.stringify(state.shopArr))
    switch( type ) {
        case "getList": 
            newArr = payload
            return {
                ...state,
                arr:newArr
            }
        case "addshop": // 加入过购物车得不能加入购物车了
           let findIndex = state.shopArr.findIndex((item,index)=>{
               // 去找数组里元素的下标，如果可以找到，返回下标，找不到，返回-1
               return item.id == payload.id

           })
           if (findIndex !== -1) {
                alert("您已经加入购物车了")
           } else {
                newShoparr.push(payload)
           }
           return {
               ...state,
               shopArr:newShoparr
           }
        case "addnum" :
            let newShoparr2 = state.shopArr.map((item,index)=>{
                if (item.id == payload) {
                    item.number ++
                }
                return item
            })
            return {
                ...state,
                shopArr:newShoparr2
            }
        case "delfn" :
            let newShoparr3 = state.shopArr.map((item,index)=>{
                if (item.id == payload) {
                    item.number -- 
                    if (item.number < 1) {
                        item.number = 1
                        alert("您不能在减去了")
                    }
                }
                return item
            })
        case "checkAll" :
            state.checkall = ! state.checkall
            let flag =  state.checkall
            let newShoparr5 = state.shopArr.map((item,index)=>{
                item.flag = flag
                return item
            })
            return {
                ...state,
                checkall:flag,
                shopArr:newShoparr5
            }
        case "checkitem" : //每一项的选中事件和反选
            let newShoparr6 = state.shopArr.map((item,index)=>{ 
                // 让当前选中得chexkbox框状态发生改变
                if(item.id == payload) {
                    item.flag = !item.flag
                }
                return item
            })
            let flagALL = newShoparr6.every((item)=>{
                return item.flag
            })
            return {
                ...state,
                shopArr:newShoparr6,
                checkall:flagALL
            }
        case "checkitem1" :
            let neworderlisrt = JSON.parse(JSON.stringify(state.orderlist))
            neworderlisrt = payload
            return {
                ...state,
                orderlist:neworderlisrt
            }
        case "getAddressList" : 
            let newAddresslist = JSON.parse(JSON.stringify(state.adrressarr))
            return {
                ...state,
                adrressarr:newAddresslist
            }
        case "addform" :
            let newAddresslist1 = JSON.parse(JSON.stringify(state.adrressarr))
            newAddresslist1.push(payload)
            return {
                ...state,
                adrressarr:newAddresslist1
            }
        case "delfn1" :
            let newAddresslist2 = JSON.parse(JSON.stringify(state.adrressarr))
            newAddresslist2.splice(payload,1)
            return {
                ...state,
                adrressarr:newAddresslist2
            }
        case "editfn" :
            let newAddresslist3 = JSON.parse(JSON.stringify(state.adrressarr))
            newAddresslist3[payload.index] = payload 
            return {
                ...state,
                adrressarr:newAddresslist3
            }
        case "onDown" :
            let newArr10 = JSON.parse(JSON.stringify(state.arr)) // 拷贝一份原始数据，为了不影响仓库里原先定义得数据
             newArr10.sort((a,b)=>{
                  return b.price - a.price
            })
            return {
                ...state,
                arr:newArr10
            }
        case "onUp" :
            let newArr11 = JSON.parse(JSON.stringify(state.arr)) // 拷贝一份原始数据，为了不影响仓库里原先定义得数据
                newArr11.sort((a,b)=>{
                    return a.price - b.price
            })
            return {
                ...state,
                arr:newArr11
            }

        default:
            return {
                ...state
            }
                
    }
    return {
        ...state
    }
}
export default shopReducer